package org.csu.newpetstoreback.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;

import java.io.Serializable;

@Getter
//JSON序列化时空的数据不被包含
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> implements Serializable {

    private int status;
    private String msg;
    private T data;

    private CommonResponse(int status){
        this.status = status;
    }

    private CommonResponse(int status,String msg){
        this.status = status;
        this.msg = msg;
    }

    private CommonResponse(int status,String msg,T data){
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    private CommonResponse(int status,T data){
        this.status = status;
        this.data = data;
    }

    //Json会把public以及非静态非构造方法序列化输出，该注解忽视JSON序列化
    @JsonIgnore
    public Boolean isSuccess(){
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    //return new CommonResponse<T>(0,); 最后不要出现0这一类常量，可维护性差
    public static <T>CommonResponse<T> createForSuccess(){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode());

    }

    public static <T>CommonResponse<T> createForSuccess(T data){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),data);

    }

    public static <T>CommonResponse<T> createForSuccessMessage(String msg){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg);

    }

    public static <T>CommonResponse<T> createForSuccess(T data,String msg){
        return new CommonResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);

    }

    public static <T>CommonResponse<T> createForNotFound(){
        return new CommonResponse<T>(ResponseCode.NOT_FOUND.getCode(),ResponseCode.NOT_FOUND.getDescription());
    }

    public static <T>CommonResponse<T> createForNotFound(String msg){
        return new CommonResponse<T>(ResponseCode.NOT_FOUND.getCode(),msg);
    }

    public static <T>CommonResponse<T> createForNotFound(int code, String msg){
        return new CommonResponse<T>(code,msg);
    }

    public static <T>CommonResponse<T> createForUnauthorized(){
        return new CommonResponse<T>(ResponseCode.UNAUTHORIZED.getCode(),ResponseCode.UNAUTHORIZED.getDescription());
    }

    public static <T>CommonResponse<T> createForUnauthorized(String msg){
        return new CommonResponse<T>(ResponseCode.UNAUTHORIZED.getCode(),msg);
    }

    public static <T>CommonResponse<T> createForUnauthorized(int code, String msg){
        return new CommonResponse<T>(code,msg);
    }


}
